<?php

namespace backend\models;

use Yii;
use common\models\Menu AS _Menu;
use creocoder\nestedsets\NestedSetsBehavior;
use yii\behaviors\TimestampBehavior;
use yii\helpers\Url;

/**
 * This is the model class for table "{{%menu}}".
 *
 * @property int $id
 * @property int $tree
 * @property int $lft
 * @property int $rgt
 * @property int $depth
 * @property string $name
 */
class Menu extends _Menu
{

    const BACKEND_TYPE  = 0;
    const FRONTEND_TYPE = 1;

    const DISPLAY_YES = 1;
    const DISPLAY_NO  = 0;

    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
            [
                'class'         => NestedSetsBehavior::className(),
                'treeAttribute' => 'tree',
            ],
        ];
    }

    public function scenarios()
    {
        return array_merge(
            parent::scenarios(),
            [
                'default' => [
                    'tree',
                    'name',
                    'url',
                    'icon',
                    'type',
                    'is_absolute_url',
                    'target',
                    'sort',
                    'is_display',
                ],
            ]
        );
    }

    public static function find()
    {
        return new MenuQuery(get_called_class());
    }


    public function fields()
    {
        $fields              = parent::fields();
        $fields['parent_id'] = function ($model) {
            return $model->parents(1)->one()['id'];
        };

        return $fields;
    }


    /**
     * @Notes   生成后台首页菜单html
     * @Interface getBackendMenu
     *
     *
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2019-12-19 16:12
     */
    public static function getBackendMenu($id)
    {
        $Parent_Menu = Menu::findOne(['id' => $id]);
        $Menus       = $Parent_Menu->children(1)->andWhere(['is_display' => self::DISPLAY_YES, 'type' => self::BACKEND_TYPE])->OrderBy(['sort' => 'ASC'])->all(
        );
        $lis         = '';
        foreach ($Menus as $key => $Menu) {
            $_Menus = $Menu->children()->all();
            foreach ($_Menus as $key => $_Menu) {
                $_Menus[$key] = $_Menu->toArray();
            }
            $subMenu      = self::_getBackendSubMenu($_Menus, $Menu['id'], '2');
            $Menu['icon'] = $Menu['icon'] ? $Menu['icon'] : 'fa-desktop';
            $Menu['url']  = self::generateUrl($Menu);
            $arrow        = '';
            $class        = 'class="J_menuItem"';
            if (!empty($subMenu)) {
                $arrow = ' arrow';
                $class = '';
            }
            $lis .= <<<EOF
<li>
    <a {$class} href="{$Menu['url']}">
        <i class="fa {$Menu['icon']}"></i>
        <span class="nav-label">{$Menu['name']}</span>
        <span class="fa {$arrow}"></span>
    </a>
    $subMenu
</li>

EOF;
        }

        return $lis;
    }

    private static function _getBackendSubMenu($menus, $cur_id, $times)
    {
        $array    = ['2' => 'second', '3' => 'third', '4' => 'fourth', '5' => 'fifth'];
        $level    = $array[$times];
        $collapse = '';
        if ($times > 2) {
            $collapse = 'collapse';
        }
        $subMenu = "<ul class='nav nav-{$level}-level {$collapse}'>";
        ++$times;
        static $i = 1;
        foreach ($menus as $menu) {
            if ($menu['parent_id'] != $cur_id) {
                continue;
            }
            $subsubmenu = self::_getBackendSubMenu($menus, $menu['id'], $times);
            $url        = $menu['url'] = self::generateUrl($menu);
            if ('' == $subsubmenu) {
                $arrow = '';
            } else {
                $arrow = '<span class="fa arrow"></span>';
            }
            $subMenu .= <<<EOF

                            <li>
                                <a class="J_menuItem" href="$url" data-index="$i"><i class="fa {$menu['icon']}"></i>{$menu['name']}{$arrow}</a>
                            $subsubmenu
                            </li>

EOF;
            ++$i;
        }
        if ($subMenu != "<ul class='nav nav-{$level}-level {$collapse}'>") {
            return $subMenu.'</ul>';
        } else {
            return '';
        }
    }

    private static function generateUrl($menu)
    {
        if ('' === $menu['url']) {
            return '';
        } else {
            if (1 == $menu['is_absolute_url']) {
                return $menu['url'];
            } else {
                return Url::to([$menu['url']]);
            }
        }
    }


}
